package com.tj.admin.auth.service;

import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.MD5;
import com.fqgj.exception.common.ApplicationException;
import com.robert.vesta.util.IdUtils;
import com.tj.admin.auth.vo.*;
import com.tj.admin.db.dao.AdminDao;
import com.tj.admin.db.dao.AdminTokenDao;
import com.tj.admin.db.entity.AdminEntity;
import com.tj.admin.db.entity.AdminTokenEntity;
import com.tj.bo.common.conf.enums.RestCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/10/18
 * Time: 4:11 下午
 */
@Service
public class LoginApplication {

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private AdminTokenDao adminTokenDao;

    /**
     * @param account
     * @param password 返回token
     */
    public LoginInfoVo login(String account, String password) {
        //查询数据库selectOnByAccessToken
        AdminEntity adminEntity = adminDao.select(new Account(account));
        if (adminEntity == null) {
            throw new ApplicationException("帐号或密码不存在");
        }
        if (!MD5.md5(password).equals(adminEntity.getPassword())) {
            throw new ApplicationException("帐号或密码不存在");
        }

        String merchantCode = adminEntity.getMerchantCode();
        //如果有token记录  逻辑删除掉
        AdminTokenEntity adminTokenEntity = adminTokenDao.selectOnByMerchantCode(adminEntity.getMerchantCode());
        if (adminTokenEntity != null){
            if (merchantCode.equals(adminTokenEntity.getMerchantCode())) {
                adminTokenDao.deleteByPrimaryKey(adminTokenEntity.getId());
            }
        }

        //生成token
        adminTokenEntity = new AdminTokenEntity()
                .setAccessToken(IdUtils.genId() + "")
                .setMerchantCode(merchantCode)
                .setExpireDate(DateUtil.addDate(DateUtil.getPatternDate(new Date(), "yyyy-MM-dd"), 30));
        adminTokenDao.insert(adminTokenEntity);

        LoginInfoVo infoVo = new LoginInfoVo(adminTokenEntity.getAccessToken(), adminEntity.getName(), adminEntity.getAvatar());
        return infoVo;
 

    }

    public void modify(String oldPwd, String newPwd, String confirmPwd) {
        //查询数据库
        AdminEntity adminEntity = adminDao.select(new Password(MD5.md5(oldPwd)));
        if (adminEntity == null) {
            throw new ApplicationException("密码不匹配");
        }

        if (!newPwd.equals(confirmPwd)) {
            throw new ApplicationException("两次密码不匹配，请检查后重新输入");
        }
        adminEntity.setPassword(MD5.md5(newPwd));
        adminDao.updateByPrimaryKey(adminEntity);
    }

    public AccountInfoVo getAccountInfo() {
        AdminTokenEntity adminTokenEntity = adminTokenDao.selectOnByAccessToken(RequestLocalInfo.getCurrentUser().getAccessToken());
        if (adminTokenEntity == null){
            throw new ApplicationException(RestCodeEnum.TOKEN_NOT_AVAILABLE);
        }
        AdminEntity adminEntity = adminDao.select(new MerchantCode(adminTokenEntity.getMerchantCode()));

        return new AccountInfoVo(adminEntity.getCompanyName(), adminEntity.getCompanyKey());
    }
}
